package com.wh.consumer8282.controller;

import com.wh.consumer8282.api.ProducerFeign;
import com.wh.consumer8282.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;


/**
 * @BelongsProject: springcloud
 * @BelongsPackage: com.bruce.controller
 * @Author: bruceliu
 * @QQ:1241488705
 * @CreateTime: 2020-03-16 16:14
 * @Description: TODO
 */
@RestController
@Scope("prototype")
public class UserController {

   @Autowired
    RestTemplate restTemplate;

   @Autowired
    DiscoveryClient discoveryClient;

   @Autowired
    ProducerFeign producerFeign;

   @Value("${aa}")
   String aa;

   @RequestMapping("aa")
   public String aa(){
       return aa;
   }
    @RequestMapping("/list")
    public List<User> user_list() {
        List<String> services = discoveryClient.getServices();
        for(String service:services){
            System.out.println(service);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("producer-8181");
        for (ServiceInstance instance:instances) {
            System.out.println(instance.getUri());
        }
        ServiceInstance serviceInstance = instances.get(0);
        /*restTemplate.getForObject("http://localhost:8181/list",List.class);*/
        return restTemplate.getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/list",List.class);
    }
    @RequestMapping("/list1")
    public List<User> user_list1() {
        return producerFeign.user_list();
    }
    @RequestMapping("/getById")
    public User getById(@RequestParam Integer id) {

        return restTemplate.getForObject("http://producer-8181/getById?id="+id,User.class);
    }

    @RequestMapping("/updateUser")
    public int updateUser(@RequestBody User user){
        System.out.println(user);
        return 1;
    }
}
